import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:gzx_dropdown_menu/gzx_dropdown_menu.dart';

import 'package:qaun_ni_dev/provider/appUserProvider.dart';
import 'package:qaun_ni_dev/pages/appUser/app_user_detail.dart';

class AppUserPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AppUserListTypeDown();
//    return Scaffold(
//      appBar: AppBar(
//        title: Text('前端用户管理'),
//        actions: <Widget>[
//          IconButton(
//            icon: Icon(Icons.label),
//            onPressed: () {
//              Navigator.push(context, MaterialPageRoute(builder: (context) {
//                return AppUserListTypeDown();
//              }));
//            },
//          )
//        ],
//      ),
//      body: Container(child: AppUserList()),
//    );
  }
}

/** -----------------------------------下拉选择菜单-------------------------**/
 // 数据类型
class SortCondition {
  String name;
  bool isSelected;
  int  typeId;
  SortCondition({this.name, this.isSelected,this.typeId});
}

class AppUserListTypeDown extends StatefulWidget {
  @override
  _AppUserListTypeDownState createState() => _AppUserListTypeDownState();
}

class _AppUserListTypeDownState extends State<AppUserListTypeDown> {

  List<String> _dropDownHeaderItemStrings = ['账号状态', '用户性别', '用户状态'];
  List<SortCondition> _stateSortConditions = []; //账号状态
  List<SortCondition> _sexSortConditions = []; // 性别
  List<SortCondition> _userInfoSortConditions = []; // 用户信息

  SortCondition _selectStateSortCondition;
  SortCondition _selectSexSortCondition;
  SortCondition _selectUserInfoSortConditions;
  GZXDropdownMenuController _dropdownMenuController = GZXDropdownMenuController(); // 实例化控制类

  var _scaffoldKey = new GlobalKey<ScaffoldState>();
  GlobalKey _stackKey = GlobalKey();

  @override
  void initState() {
    super.initState();

    _stateSortConditions.add(SortCondition(typeId: 0,name: '全部',isSelected: true));
    _stateSortConditions.add(SortCondition(typeId: 0,name: '正常',isSelected: false));
    _stateSortConditions.add(SortCondition(typeId: 1,name: '封禁',isSelected: false));
    _selectStateSortCondition = _stateSortConditions[0];

    _sexSortConditions.add(SortCondition(typeId: 0,name: '全部',isSelected: true));
    _sexSortConditions.add(SortCondition(typeId: 0,name: '女',isSelected: false));
    _sexSortConditions.add(SortCondition(typeId: 2,name: '男',isSelected: false));
    _selectSexSortCondition = _sexSortConditions[0];



    _userInfoSortConditions.add(SortCondition(typeId: 0,name: '全部',isSelected: true));
    _userInfoSortConditions.add(SortCondition(typeId: 0,name: '选择性别',isSelected: false));
    _userInfoSortConditions.add(SortCondition(typeId: 1,name: '上传照片',isSelected: false));
    _userInfoSortConditions.add(SortCondition(typeId: 2,name: '自拍视镜',isSelected: false));
    _userInfoSortConditions.add(SortCondition(typeId: 3,name: '等待审核',isSelected: false));
    _userInfoSortConditions.add(SortCondition(typeId: 4,name: '选择女性',isSelected: false));
    _userInfoSortConditions.add(SortCondition(typeId: 5,name: '充值',isSelected: false));
    _userInfoSortConditions.add(SortCondition(typeId: 6,name: '审核不通过',isSelected: false));
    _userInfoSortConditions.add(SortCondition(typeId: 7,name: '审核通过',isSelected: false));
    _selectUserInfoSortConditions = _userInfoSortConditions[0];
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text('app用户列表'),
      ),
      body: Stack(
        key: _stackKey,
        children: <Widget>[
          Column(
            children: <Widget>[
              GZXDropDownHeader(
                items: [
                  GZXDropDownHeaderItem(_dropDownHeaderItemStrings[0]),
                  GZXDropDownHeaderItem(_dropDownHeaderItemStrings[1]),
                  GZXDropDownHeaderItem(_dropDownHeaderItemStrings[2]),
                ],
                stackKey: _stackKey,
                controller: _dropdownMenuController,
                onItemTap: (index) {
                  print('当前索引${index}');
//                  if (index == 3) {
//                    _scaffoldKey.currentState.openEndDrawer();
//                  }
                },
              ),
              Container(
                child: Expanded(
                  child: AppUserList()
                ),
              )
            ],
          ),
          GZXDropDownMenu(
            controller: _dropdownMenuController,
            animationMilliseconds: 500,
            menus: [
              GZXDropdownMenuBuilder(
                dropDownHeight: 40 * 8.0,
                dropDownWidget: _buildConditionListWidget(_stateSortConditions,(value){
                  _selectStateSortCondition = value;
                  _dropDownHeaderItemStrings[0] = _selectStateSortCondition.name == '全部'? '账号状态':_selectStateSortCondition.name;
                  _dropdownMenuController.hide();
                  setState(() {
                    if(_selectStateSortCondition.name == '全部') {
                      Provider.of<AppUserProvide>(context,listen: false).changeStateValue('');
                      Provider.of<AppUserProvide>(context,listen: false).getOnRefreshList(context);
                    } else {
                      Provider.of<AppUserProvide>(context,listen: false).changeStateValue(_selectStateSortCondition.typeId);
                      Provider.of<AppUserProvide>(context,listen: false).getOnRefreshList(context);
                    }
                    print('${_selectStateSortCondition.typeId}');
                  });
                })
              ),
              GZXDropdownMenuBuilder(
                  dropDownHeight: 40 * 8.0,
                  dropDownWidget: _buildConditionListWidget(_sexSortConditions,(value){
                    _selectSexSortCondition = value;
                    _dropDownHeaderItemStrings[1] = _selectSexSortCondition.name == '全部'? '性别':_selectSexSortCondition.name;
                    _dropdownMenuController.hide();
                    setState(() {
                      if(_selectSexSortCondition.name == '全部') {
                        Provider.of<AppUserProvide>(context,listen: false).changeSexValue('');
                        Provider.of<AppUserProvide>(context,listen: false).getOnRefreshList(context);
                      } else {
                        Provider.of<AppUserProvide>(context,listen: false).changeSexValue(_selectSexSortCondition.typeId);
                        Provider.of<AppUserProvide>(context,listen: false).getOnRefreshList(context);
                      }
                      print('${_selectSexSortCondition.typeId}');
                    });
                  })
              ),
              GZXDropdownMenuBuilder(
                  dropDownHeight: 40 * 8.0,
                  dropDownWidget: _buildConditionListWidget(_userInfoSortConditions,(value){
                    _selectUserInfoSortConditions = value;
                    _dropDownHeaderItemStrings[2] = _selectUserInfoSortConditions.name == '全部'? '用户状态':_selectUserInfoSortConditions.name;
                    _dropdownMenuController.hide();
                    setState(() {
                      if(_selectUserInfoSortConditions.name == '全部') {
                        Provider.of<AppUserProvide>(context,listen: false).changeInfoState('');
                        Provider.of<AppUserProvide>(context,listen: false).getOnRefreshList(context);
                      } else {
                        Provider.of<AppUserProvide>(context,listen: false).changeInfoState(_selectUserInfoSortConditions.typeId);
                        Provider.of<AppUserProvide>(context,listen: false).getOnRefreshList(context);
                      }
                      print('${_selectUserInfoSortConditions.typeId}');
                    });
                  })
              ),

            ],
          )

        ],
      ),
    );
  }

  _buildConditionListWidget(items, void itemOnTap(SortCondition sortCondition)) {
    return ListView.builder(
        shrinkWrap: true,
        scrollDirection: Axis.vertical,
        itemCount: items.length,
        itemBuilder: (BuildContext context,int index) {
          SortCondition typeSortCondition = items[index];
          return InkWell(
            onTap: () {
              for (var value in items) {
                value.isSelected = false;
              }
              typeSortCondition.isSelected = true;
              itemOnTap(typeSortCondition);
            },
            child: Container(
              height: 40,
              child: Row(
                children: <Widget>[
                  SizedBox(
                    width: 16,
                  ),
                  Expanded(
                    child: Text(
                      typeSortCondition.name,
                      style: TextStyle(
                        color: typeSortCondition.isSelected ? Colors.black26: Colors.black
                      ),
                    ),
                  ),
                  typeSortCondition.isSelected ? Icon(Icons.check):SizedBox(),
                  SizedBox(
                    width: 16,
                  )
                ],
              ),
            ),
          );

        },
        );
  }
}

















// 实际的app用户列表数据
class AppUserList extends StatefulWidget {
  @override
  _AppUserListState createState() => _AppUserListState();
}
class _AppUserListState extends State<AppUserList> {
  GlobalKey<RefreshFooterState> _footerKey2 = new GlobalKey<RefreshFooterState>();
  GlobalKey<RefreshHeaderState> _headerKey2 = new GlobalKey<RefreshHeaderState>();

  void initState() {
    super.initState();
    _getMoreList();
  }

  void _getMoreList() {
    Provider.of<AppUserProvide>(context, listen: false)
        .getAppUserDataList(context);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 620,
      padding: EdgeInsets.only(top: 10, left: 15, right: 15),
      child: Consumer<AppUserProvide>(
        builder: (context, data, _) {
          if (data.appUserDataList.length > 0) {
            return Container(
              child: EasyRefresh(
                  refreshFooter: ClassicsFooter(
                      key: _footerKey2,
                      bgColor: Colors.white,
                      textColor: Colors.pink,
                      moreInfoColor: Colors.pink,
                      showMore: true,
                      noMoreText: '没有更多了',
                      moreInfo: '加载中',
                      loadReadyText: '上拉加载'),
                  refreshHeader: ClassicsHeader(
                      key: _headerKey2,
                      refreshText: "下拉刷新数据",
                      refreshReadyText: "准备刷新数据",
                      refreshingText: "数据刷新中...",
                      refreshedText: "刷新完成",
                      bgColor: Colors.pink,
                      textColor: Colors.white,
                      moreInfoColor: Colors.white,
                      refreshHeight: 70.0,
                      isFloat: false,
                      showMore: false,
                      moreInfo: "Updated at %T"),
                  child: ListView.builder(
                      itemCount: data.appUserDataList.length,
                      itemBuilder: (context, index) {
                        return _listWidget(data.appUserDataList, index);
                      }),
                  loadMore: () async {
                    print('加载更多的');
                    Provider.of<AppUserProvide>(context, listen: false).getAppUserMoreList(context);
                  },
                  onRefresh: () async {
                    print('上拉刷新');
                    Provider.of<AppUserProvide>(context, listen: false).getOnRefreshList(context);
                  }),
            );
          } else {
            return Text('请稍等，数据加载中或暂无数据');
          }
        },
      ),
    );
  }

  Widget _listWidget(List newLists, int index) {
    return InkWell(
      onTap: () {
//        print('点击了---->>>>：${newLists[index]}');
        Navigator.push(context, MaterialPageRoute(builder: (context) {
//          return Text(newList[index].id);
          return AppUserDetail(newLists[index]);
        }));
      },
      child: Container(
        margin: EdgeInsets.only(bottom: 10),
        decoration: BoxDecoration(
            border: Border.all(width: 1, color: Colors.black12),
            borderRadius: BorderRadius.all(Radius.circular(6))),
        child: Row(
          children: <Widget>[
            _leftOrderNumber(newLists, index),
            _rightContent(newLists, index),
          ],
        ),
      ),
    );
  }

  Widget _leftOrderNumber(newLists, index) {
    return Container(
      margin: EdgeInsets.only(right: 10),
      padding: EdgeInsets.only(left: 6),
      width: 50,
      child: Column(
        children: <Widget>[
          Text(
            '序号${index}',
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }

  Widget _rightContent(newLists, index) {
    return Container(
      padding: EdgeInsets.only(left: 8, right: 6, top: 6, bottom: 6),
      decoration: BoxDecoration(
          border: Border(
        left: BorderSide(color: Colors.grey, width: 1),
      )),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '用户昵称:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${newLists[index].nickName ==null? '暂无昵称':newLists[index].nickName }'),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '用户性别:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${newLists[index].sex == 0? '女': '男'}'),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '账号状态:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${newLists[index].accountStatu == 0? '正常': '封禁'}'),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
